package com.burgeon.backend.Rest.User.item;
/**
 * 获取积分流水
 */
import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.entity.RechargeItem;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class FindItemList extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public FindItemList(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     */
    public ArrayList<RechargeItem> getData(String phone) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(phone), url);

        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!code.equals("0")){
            return null;
        }
        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<RechargeItem> list = new ArrayList<RechargeItem>();

        for (int i = 0; i < jsonArray.length(); i++) {
            JSONArray array = jsonArray.getJSONArray(i);
            RechargeItem rechargeItem = new RechargeItem();
            rechargeItem.setId(array.getString(0));
            String date = array.getString(1);
            rechargeItem.setDate(date.substring(0,date.length()-2));
            rechargeItem.setCode(array.getString(2));
            rechargeItem.setIntegral(array.getString(3));
            rechargeItem.setPrice(array.getString(4));
            rechargeItem.setStore(array.getString(5));
            if (rechargeItem.getIntegral().equals("0")&&rechargeItem.getPrice().equals("0")){
                continue;
            }

            list.add(rechargeItem);
        }
        return list;

    }

    private MultiValueMap Data(String code) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("ID");
        columns.add("CREATIONDATE");//日期
        columns.add("DOCNO");//单据编号
        columns.add("INTEGRAL");//积分
        columns.add("VIP_PAYAMT");//金额
        columns.add("'C_STORE_ID;NAME'");//店仓

        params.put("columns", columns);
        params.put("table", "FA_VIPINTEGRAL_FTP");//表名

        params.put("count", "true");
        HashMap paramss = new HashMap();
        String ac = "'" + code + "'";
        paramss.put("column", "'C_VIP_ID;CARDNO'");
        paramss.put("condition", ac);//查询条件

        HashMap orderby = new HashMap();
        orderby.put("column", "ID");
        orderby.put("asc", "false");
        ArrayList list = new ArrayList();
        list.add(orderby);

        params.put("orderby", list);

        params.put("params", paramss);
        params.put("range", "2000");

        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        FindItemList g = new FindItemList("47.92.142.10:55321", "jiekou1@tsnw.com", "123456");
        try {
           g.getData("17732141036");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
